# -*- encoding: utf-8 -*-
"""
File Name: router_config_parse
Description:
Author : 'lirui'
date: 2024/3/5
Contact: marjey_lee@163.com
Change Activity:
2024/3/5:
"""
import ll


def _split_lines(content):
    """

    Args:
        content:

    Returns:

    """
    lines = content.split('\n')
    items = []
    item = []
    start_content = False
    for line in lines:
        if line.startswith('interface '):
            start_content = True
            item = []
        if start_content and line.startswith('#'):
            start_content = False
            items.append(item)
            item = []
        if start_content:
            item.append(line)
    return items


def _parse_content(items):
    parse_res = []
    for item in items:
        if len(item) == 1:
            continue
        obj = {}
        for line in item:
            if line.startswith('interface'):
                obj['interface'] = line
            elif line.startswith(' ip address'):
                obj['local_address'] = line.split(' ')[3]
        if 'local_address' in obj and 'interface' in obj:
            parse_res.append(obj)
    return parse_res


def parse_router_config(content):
    """
    parse router config
    Args:
        content:

    Returns:

    """
    items = _split_lines(content)
    parse_ = _parse_content(items)
    return parse_


def _main():
    p = ll.join_relative_path(
        'projects/dedicated_line_monitor/resource/router_config_template.txt'
    )
    content = ll.try_read_file_text(p)
    parse_router_config(content)
    pass


if __name__ == '__main__':
    _main()
